define(['moment'], function () {
    return ['$scope', '$rootScope', '$filter', '$stateParams', '$mdDialog', 'config', 'MMWService', '$calculater',
        function ($scope, $rootScope, $filter, $stateParams, $mdDialog, config, $MMWService, $calculater) {

            $scope.init = function () {
                $scope.work_type = 'P';
                $scope.equipment_no = '';
                $scope.lot_no = '';
                $scope.reasono_of_overTime = '';
                $scope.over_time_list=[];
                $scope.warn_list=[];

                // fake_info
                // $scope.warn_list=[
                //     {
                //         id:0,
                //         guid:"11101",
                //         plot_no:'MO1234',
                //         seq:"3",
                //         before_seq:"5",
                //         time_interval:'20min',
                //         real_time_interval:'20min'
                //     },
                //     {
                //         id:1,
                //         guid:"11102",
                //         plot_no:'MO1234',
                //         seq:"3",
                //         before_seq:"5",
                //         time_interval:'20min',
                //         real_time_interval:'20min'
                //     },{
                //         id:2,
                //         guid:"11103",
                //         plot_no:'MO1234',
                //         seq:"3",
                //         before_seq:"5",
                //         time_interval:'20min',
                //         real_time_interval:'20min'
                //     },{
                //         id:3,
                //         guid:"11104",
                //         plot_no:'MO1234',
                //         seq:"3",
                //         before_seq:"5",
                //         time_interval:'20min',
                //         real_time_interval:'20min'
                //     },{
                //         id:4,
                //         guid:"11105",
                //         plot_no:'MO1234',
                //         seq:"3",
                //         before_seq:"5",
                //         time_interval:'20min',
                //         real_time_interval:'20min'
                //     },{
                //         id:5,
                //         guid:"11106",
                //         plot_no:'MO1234',
                //         seq:"3",
                //         before_seq:"5",
                //         time_interval:'20min',
                //         real_time_interval:'20min'
                //     },
                // ]
            };

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            //20170505 modify by Dustdusk for 入庫查詢可以不用輸入條件
            $scope.confirm = function () {
                if($scope.over_time_reason_no && $scope.over_time_reason_name){//已選超時原因
                    if($scope.over_time_list.length == 0){
                        $scope.$root.showAlert($filter('translate')('SMT24.msg.no_overTime_data'));
                    }else{
                        let content = {
                            reason_no:$scope.over_time_reason_no,
                            reason_name:$scope.over_time_reason_name,
                            result:$scope.work_type,
                            warn_list:$scope.over_time_list
                        }
                        $MMWService.sendToServer({
                            uri: 'ServicesSMT.Module_SMT.partial_warn_process',
                            content : content,
                            success : function(data){
                                $scope.over_time_list = [];
                                $scope.$root.showToast($filter('translate')('common.msg.success'));
                            }
                        });
                    }
                }else{
                    $scope.$root.showAlert($filter('translate')('SMT24.msg.chose_reason_plz'));
                }
            };

            $scope.add_warn_item = function(item){
                let over_time_item = angular.copy(item);
                $scope.warn_list.splice($scope.warn_list.indexOf(item), 1);
                $scope.over_time_list.unshift(over_time_item);

            }

            $scope.chengeType = function(type){
                $scope.work_type = type;
            }

            $scope.search = function () {
                let content = {
                    input_no:$scope.lot_no,
                    eqp_no:$scope.equipment_no
                }
                $MMWService.sendToServer({
                    uri: 'ServicesSMT.Module_SMT.partial_warn_info_get',
                    content : content,
                    success : function(data){
                        $scope.warn_list = data.warn_list;
                        $scope.lot_no = '';
                        $scope.equipment_no = '';
                        $scope.warn_list.forEach((item,key)=>{//為每個項目增加排序，方便上下移動
                            item.id = key;
                        });
                    }
                });
            };

            // 設備編號查詢
            $scope.op_no_search = function () {
                $MMWService.showQueryListPNIG({
                    page_mode: 'smt_equipment_info_get',
                    key: 'equipment_no',
                    header: [
                        { label: $filter('translate')('SMT24.page1.op_no'), code: 'equipment_no' },
                        { label: $filter('translate')('SMT24.page1.op_name'), code: 'equipment_name' }
                    ],
                    confirm: function (selectItem, dialog) {
                        $scope.equipment_no = selectItem.equipment_no;
                        dialog.hide();
                    }
                });
            };

            //刪除項目
            $scope.removeItem = function(item){
                if($scope.warn_list.length > item.id){
                    $scope.warn_list.splice(item.id,0,item);
                    $scope.over_time_list.splice($scope.over_time_list.indexOf(item), 1);
                }else{
                    $scope.warn_list.push(item);
                    $scope.over_time_list.splice($scope.over_time_list.indexOf(item), 1);
                }
            }

            // 設備編號查詢
            $scope.over_time_reason_search = function () {
                $MMWService.showQueryListPNIG({
                    page_mode: 'smt_post_warn_reason',
                    key: 'reason_no',
                    header: [
                        { label: $filter('translate')('SMT24.page2.reason_no'), code: 'reason_no' },
                        { label: $filter('translate')('SMT24.page2.reason_name'), code: 'reason_name' }
                    ],
                    confirm: function (selectItem, dialog) {
                        $scope.over_time_reason_no = selectItem.reason_no;
                        $scope.over_time_reason_name = selectItem.reason_name;
                        dialog.hide();
                    }
                });
            };

            // 開啟相機掃QRCode
            $scope.QRCodeScanner = function(target){
                $scope.$root.OpenScanner({
                    after : function(result){
                        target = result.text;
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('B0214.showAlert.column3'));
                    },
                    orientation : 'landscape'
                });
            }

        }
    ];
});
